//
//  SelectTableHeaderView.m
//  礼物go
//
//  Created by dllo on 16/5/18.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "SelectTableHeaderView.h"
#import "HeaderCollectionViewCell.h"
#import "SecondaryCollectionModel.h"
#import "CycleColumnViewController.h"

static NSString *const secondaryCell = @"secondaryCo";

@interface SelectTableHeaderView ()<UICollectionViewDataSource,UICollectionViewDelegate>


@end

@implementation SelectTableHeaderView

-(instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        self.dataArray = [NSMutableArray array];
        self.imageArray = [NSMutableArray array];
        self.secondCollArray = [NSMutableArray array];
        self.dk_backgroundColorPicker = DKColorPickerWithRGB(0xFFFAFA,0xBEBEBE);
        [self createSubviews];
    }
    return self;
}


-(void)createSubviews{
    self.cycleScroll = [[LYCycleScrollView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height / 3 * 2 - 5)];
   // self.cycleScroll.backgroundColor = [UIColor grayColor];
    [self addSubview:self.cycleScroll];
    
    NSString *urlString = @"http://api.liwushuo.com/v2/banners?channel=iOS";
    [LYNetworking ly_GetWithURL:urlString Parameter:nil success:^(id result) {
        NSDictionary *dataDic = [result objectForKey:@"data"];
        NSArray *bannersArray = [dataDic objectForKey:@"banners"];
        for (NSDictionary *dic in bannersArray) {
            CycleViewModel *cycleView = [[CycleViewModel alloc] init];
            [cycleView setValuesForKeysWithDictionary:dic];
            [self.dataArray addObject:cycleView];
            [self.imageArray addObject:cycleView.image_url];
        }
        [self.cycleScroll setImages:self.imageArray];
        __block SelectTableHeaderView *headerView = self;
        self.cycleScroll.pushBlock = ^(NSInteger index){
            CycleViewModel *cycleModel = [headerView.dataArray objectAtIndex:index];
            headerView.selPushBlock(cycleModel);
        };
    } error:^(NSError *error) {
        
        
    }];
       [self createCollectionView];
    
}

-(void)createCollectionView{
UICollectionViewFlowLayout *flowLayOut = [[UICollectionViewFlowLayout alloc] init];
    flowLayOut.itemSize = CGSizeMake((self.frame.size.width) / 4, self.frame.size.height / 3 - 6);
    flowLayOut.sectionInset = UIEdgeInsetsMake(3, 10, 3, 0);
    flowLayOut.scrollDirection = UICollectionViewScrollDirectionHorizontal;

    self.headerCollection = [[UICollectionView alloc] initWithFrame:CGRectMake(0, self.frame.size.height / 3 * 2, self.frame.size.width, self.frame.size.height / 3) collectionViewLayout:flowLayOut];
    [self addSubview:self.headerCollection];
    self.headerCollection.dk_backgroundColorPicker = DKColorPickerWithRGB(0xFFFAFA,0xBEBEBE);
    self.headerCollection.showsHorizontalScrollIndicator = NO;
    self.headerCollection.dataSource = self;
    self.headerCollection.delegate = self;
    [self.headerCollection registerClass:[HeaderCollectionViewCell class] forCellWithReuseIdentifier:secondaryCell];
    
    
    NSString *urlStr = @"http://api.liwushuo.com/v2/secondary_banners?gender=2&generation=4";
    [LYNetworking ly_GetWithURL:urlStr Parameter:nil success:^(id result) {
        NSDictionary *dataDic = [result objectForKey:@"data"];
        NSArray *secondArray = [dataDic objectForKey:@"secondary_banners"];
        for (NSDictionary *dic in secondArray) {
            SecondaryCollectionModel *secondModel = [[SecondaryCollectionModel alloc] init];
            [secondModel setValuesForKeysWithDictionary:dic];
            [self.secondCollArray addObject:secondModel];
           
        }
        
        [self.secondCollArray removeObjectAtIndex:3];
        [self.secondCollArray removeObjectAtIndex:2];
        [self.secondCollArray removeObjectAtIndex:1];
        
        [self.headerCollection reloadData];
        
    } error:^(NSError *error) {
        
        
    }];
    
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
HeaderCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:secondaryCell forIndexPath:indexPath];
    SecondaryCollectionModel *secondary = [self.secondCollArray objectAtIndex:indexPath.row];
    cell.dk_backgroundColorPicker = DKColorPickerWithRGB(0xFFFAFA,0xBEBEBE);
    cell.secondary = secondary;
    
    return cell;
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.secondCollArray.count;
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    if (!(indexPath.row == 1 || indexPath.row == 2 || indexPath.row == 3 || indexPath.row == 8)) {
        SecondaryCollectionModel *secondary = [self.secondCollArray objectAtIndex:indexPath.row];
        NSString *target = secondary.target_url;
        NSInteger length = [target length];
        NSString *dataId = [target substringFromIndex:length - 3];
       // NSNumber *numberId = @([dataId integerValue]);
        self.cycPushBlock(dataId);
    }
    
    if (indexPath.row == 8) {
        SecondaryCollectionModel *secondary = [self.secondCollArray objectAtIndex:indexPath.row];
        NSString *target = secondary.target_url;
        NSInteger length = [target length];
        NSString *dataId = [target substringFromIndex:length - 2];
         self.cycPushBlock(dataId);
    }

}


/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
